<?php
namespace common\components\behaviors;
use yii\base\Behavior;
use common\components\bases\BaseModel;
use yii\db\Query;
use common\components\helpers\Fun;
class SynchronousAccessBehavior extends Behavior{

    public function events()
    {
        return [
            BaseModel::EVENT_AFTER_UPDATE => 'afterUpdate',
            BaseModel::EVENT_AFTER_DELETE => 'afterDelete',
        ];
    }

	public function afterUpdate($event){
        $auth=\Yii::$app->authManager;
        if(array_key_exists('m',$event->changedAttributes)||array_key_exists('c',$event->changedAttributes)||array_key_exists('a',$event->changedAttributes)||array_key_exists('param',$event->changedAttributes)){
            if($this->owner->module == 'main'){
                $menu_cache=Fun::cache('menu_main_cache');
                $auth_name=Fun::getPermissionStr($menu_cache[$this->owner->id]);
                $menu=(new Query())->select('name')->from('{{%auth_item}}')->where(['menu_id'=>$this->owner->id])->one();
                if($menu != false){
                    \Yii::$app->db->createCommand()->update('{{%auth_item}}',['name'=>$auth_name],['menu_id'=>$this->owner->id])->execute();
                    \Yii::$app->db->createCommand()->update('{{%auth_item_child}}',['child'=>$auth_name],['child'=>$menu])->execute();
                }else{
                    if($this->owner->parent_id==0){
                        $type=1;
                    }else{
                        $type=2;
                    }
                    \Yii::$app->db->createCommand()->insert('{{%auth_item}}',['name'=>$auth_name,'type'=>$type,'description'=>$this->owner->name,'rule_name'=>null,'data'=>null,'created_at'=>time(),'updated_at'=>time(),'menu_id'=>$this->owner->id])->execute();
                }
            }elseif($this->owner->module == 'branch'){
                $menu_cache=Fun::cache('menu_branch_cache');
                $auth_name=Fun::getPermissionStr($menu_cache[$this->owner->id]);
                $dbs_info =\Yii::$app->params['db'];
                foreach($dbs_info as $k=>$v){
                    $db = \Yii::createObject($v);
                    $menu=(new Query())->select('name')->from('{{%auth_item}}')->where(['menu_id'=>$this->owner->id])->one($db);
                    if($menu != false){
                        $db->createCommand()->update('{{%auth_item}}',['name'=>$auth_name],['menu_id'=>$this->owner->id])->execute();
                        $db->createCommand()->update('{{%auth_item_child}}',['child'=>$auth_name],['child'=>$menu])->execute();
                    }else{
                        if($this->owner->parent_id==0){
                            $type=1;
                        }else{
                            $type=2;
                        }
                        $db->createCommand()->insert('{{%auth_item}}',['name'=>$auth_name,'type'=>$type,'description'=>$this->owner->name,'rule_name'=>null,'data'=>null,'created_at'=>time(),'updated_at'=>time(),'menu_id'=>$this->owner->id])->execute();
                    }
                }
            }
        }
        if(array_key_exists('name',$event->changedAttributes)){
            \Yii::$app->db->createCommand()->update('{{%auth_item}}',['description'=>$this->owner->name],['menu_id'=>$this->owner->id])->execute();
        }
        $p=$auth->createPermission('test_test');//测试
        $auth->add($p);
        $auth->remove($p);
	}
	
	public function afterDelete($event){
		$auth=\Yii::$app->authManager;
        if($this->owner->module=='main'){
            $menu_cache=Fun::cache('menu_main_cache');
            $auth_name=Fun::getPermissionStr($menu_cache[$this->owner->id]);
            $permission=$auth->getPermission($auth_name);
            if($permission!==null)$auth->remove($permission);
        }elseif($this->owner->module=='branch'){
            $menu_cache=Fun::cache('menu_branch_cache');
            $auth_name=Fun::getPermissionStr($menu_cache[$this->owner->id]);
            $dbs_info =\Yii::$app->params['db'];
            foreach($dbs_info as $k=>$v) {
                $db = \Yii::createObject($v);
                $permission=$auth->getPermission($auth_name);
                if($permission!==null) $db->createCommand()->delete('{{%auth_item_child}}',['menu_id'=>$this->owner->id]);
            }
        }
	}

	
}
?>